package com.bjb.vr.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjb.vr.common.ao.SysConfigAo;
import com.bjb.vr.common.ao.UmsAccountAo;
import com.bjb.vr.common.constant.ThreadLocalConstant;
import com.bjb.vr.common.dto.SysConfig;
import com.bjb.vr.common.exception.CommonException;
import com.bjb.vr.common.mapper.SysConfigMapper;
import com.bjb.vr.common.utils.ThreadLocalUtil;
import com.bjb.vr.common.vo.DevResSyncStatusVo;
import com.bjb.vr.common.vo.UmsAccountVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Objects;

/**
 * @author admin
 * @description 针对表【sys_config】的数据库操作Service实现
 * @createDate 2022-05-07 18:05:56
 */
@Service
public class SysConfigService extends ServiceImpl<SysConfigMapper, SysConfig> {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 更新系统配置
     *
     * @param sysConfigAo
     * @return
     */
    public boolean updateSystemConfig(SysConfigAo sysConfigAo) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setId(sysConfigAo.getId());
        sysConfig.setCountOfRing(sysConfigAo.getCountOfRing());
        sysConfig.setOfflineTime(sysConfigAo.getOfflineTime());
        sysConfig.setStuOfflineTime(sysConfigAo.getStuOfflineTime());
        return this.updateById(sysConfig);
    }


    /**
     * 客户配置管理-设备资源同步开关
     *
     * @param sysConfigAo
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateDevResSyncStatus(SysConfigAo sysConfigAo) {
        Assert.notNull(sysConfigAo.getTenantId(), "租户Id不能为空!");
        Assert.notNull(sysConfigAo.getDevUptState(), "同步状态不能为空!");
        ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, true);
        try {
            LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SysConfig::getTenantId, sysConfigAo.getTenantId());
            SysConfig sysConfig = sysConfigMapper.selectOne(lambdaQueryWrapper);
            if (sysConfig != null) {
                sysConfig.setDevUpdState(sysConfigAo.getDevUptState());
                return sysConfigMapper.updateById(sysConfig) == 1;
            } else {
                UmsAccountVo umsAccountVo = (UmsAccountVo) ThreadLocalUtil.get(ThreadLocalConstant.USER_ACCOUNT_INFO);
                umsAccountVo.setTempTenantId(sysConfigAo.getTenantId());

                SysConfig _sysconfig = new SysConfig();
                _sysconfig.setDevUpdState(sysConfigAo.getDevUptState());
                return this.save(_sysconfig);
            }
        } catch (Exception ex) {
            throw new CommonException(ex.getMessage());
        } finally {
            ThreadLocalUtil.set(ThreadLocalConstant.NO_TENANT_ID_VERIFY, false);
        }
    }


    public DevResSyncStatusVo getDevResSyncStatus(SysConfigAo sysConfigAo) {
        Assert.notNull(sysConfigAo.getTenantId(), "租户Id不能为空!");
        LambdaQueryWrapper<SysConfig> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SysConfig::getTenantId, sysConfigAo.getTenantId());
        SysConfig sysConfig = sysConfigMapper.selectOne(lambdaQueryWrapper);
        DevResSyncStatusVo devResSyncStatusVo = null;
        if (sysConfig != null) {
            devResSyncStatusVo = new DevResSyncStatusVo();
            devResSyncStatusVo.setDevUpdState(sysConfig.getDevUpdState());
            devResSyncStatusVo.setTenantId(sysConfig.getTenantId());
        }
        return devResSyncStatusVo;
    }


    /**
     * 查询系统设置
     */
    public SysConfig querySystemConfig(SysConfigAo sysConfigAo) {
        return this.getOne(new LambdaQueryWrapper<>());
    }

    /**
     * 添加系统配置
     *
     * @param umsAccountAo
     */
    public void addTenantInfo(UmsAccountAo umsAccountAo) {
        LambdaQueryWrapper<SysConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(umsAccountAo.getTenantId()), SysConfig::getTenantId, umsAccountAo.getTenantId());
        SysConfig selectOne = sysConfigMapper.selectOne(queryWrapper);
        if (Objects.isNull(selectOne)) {
            SysConfig sysConfig = new SysConfig();
            sysConfig.setTenantId(umsAccountAo.getTenantId());
            sysConfigMapper.insert(sysConfig);
        }
    }
}




